updating oE approx

Floating Point

approx

include math.e 
namespace math 
public function approx(object p, object q, atom epsilon = 0.005) 

compares two (sets of) numbers based on approximate equality.

Parameters:
  1. p : an object, one of the sets to consider
  2. q : an object, the other set.
  3. epsilon : an atom used to define the amount of inequality allowed. This must be a positive value. Default is 0.005
Returns:

An integer,

  • 1 when p > (q + epsilon) : P is definitely greater than q.
  • -1 when p < (q - epsilon) : P is definitely less than q.
  • 0 when p >= (q - epsilon) and p <= (q + epsilon) : p and q are approximately equal.
Comments:

This can be used to see if two numbers are near enough to each other.

Also, because of the way floating point numbers are stored, it not always possible express every real number exactly, especially after a series of arithmetic operations. You can use approx to see if two floating point numbers are almost the same value.

If p and q are both sequences, they must be the same length as each other.

If p or q is a sequence, but the other is not, then the result is a sequence of results whose length is the same as the sequence argument.

Example 1:
? approx(10, 33.33 * 30.01 / 100)  
          --> 0 because 10 and 10.002333 are within 0.005 of each other 
? approx(10, 10.001)  
          --> 0 because 10 and 10.001 are within 0.005 of each other 
? approx(10, {10.001,9.999, 9.98, 10.04})  
          --> {0,0,1,-1} 
? approx({10.001,9.999, 9.98, 10.04}, 10)  
          --> {0,0,-1,1} 
? approx({10.001,{9.999, 10.01}, 9.98, 10.04}, {10.01,9.99, 9.8, 10.4})  
          --> {-1,{1,1},1,-1} 
? approx(23,32, 10)  
          --> 0 because 23 and 32 are within 10 of each other. 
Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu